package com.zoe.onelink.auth.token.processor.impl;

import com.zoe.onelink.auth.property.OnelinkAuthProperties;
import com.zoe.onelink.auth.token.processor.OnelinkTokenInfoProcessor;
import com.zoe.onelink.common.constant.OnelinkConstant;
import com.zoe.onelink.common.enumeration.ExceptionCodeEnum;
import com.zoe.onelink.common.exception.ApiException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Map;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2022</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2022-05-12
 */
@RequiredArgsConstructor
public class OnelinkRedisTokenInfoProcessor implements OnelinkTokenInfoProcessor {

    private final RedisTemplate<String, Object> redisTemplate;

    @Override
    public Map<String, Object> process(String onelinkToken) {
        Map<String, Object> authInfo = getTokenFromRedis(onelinkToken);
        // 权限信息为空说明未登录
        if (authInfo.isEmpty()) {
            throw ApiException.createEx(ExceptionCodeEnum.TOKEN_AUTH_INFO_NOT_BE_EMPTY);
        }
        return authInfo;
    }

    private Map<String, Object> getTokenFromRedis(String onelinkToken) {
        String rdsKey = OnelinkConstant.AUTH_TOKEN_REDIS_KEY_PREFIX + onelinkToken;
        return this.getAuthInfo(rdsKey);
    }

    private Map<String, Object> getAuthInfo(String rdsKey) {
        return this.redisTemplate.<String, Object>opsForHash().entries(rdsKey);
    }

}
